package com.alm.token.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.alm.token.R;


/**
 * Created by Nelson on 15/11/14.
 */
public class CustomTabLayout extends LinearLayout {

    private String[] tabNames;

    private LinearLayout mWholeLayout;

    public CustomTabLayout(Context context) {
        super(context);
    }

    public CustomTabLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        initView(attributeSet, context);
    }

    public CustomTabLayout(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        initView(attributeSet, context);
    }

    private void initView(AttributeSet attributeSet, Context context) {
        mWholeLayout = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.widget_tab_layout, this);
    }

    public String[] getTabNames() {
        return tabNames;
    }

    public void setTabNames(String[] tabNames) {
        this.tabNames = tabNames;
    }

    public void notifyChange() {

        mWholeLayout.removeAllViews();
        if (tabNames == null) {
            throw new NullPointerException("tab data is null");
        }

        for (int i = 0; i < tabNames.length; i++) {
            String tabName = tabNames[i];

            final FrameLayout item = (FrameLayout) LayoutInflater.from(getContext()).inflate(getItemLayout(), null);
            LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            layoutParams.weight = 1;
            item.setLayoutParams(layoutParams);
            TextView name = (TextView) item.findViewById(R.id.tab_name);
            name.setText(tabName);
            item.setTag(String.valueOf(i));
            item.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    int id = Integer.parseInt(String.valueOf(view.getTag()));
                    if(tabCall!=null){
                        tabCall.item(id,item);
                    }
                    setSelectedView(id);
                    if (viewPager != null) {
                        viewPager.setCurrentItem(id);
                    }

                }
            });

            mWholeLayout.addView(item);
        }

        setSelectedView(0);
    }

    public int getItemLayout() {
        if (itemLayout == 0) {
            return R.layout.item_wechat_tab;
        } else {
            return itemLayout;
        }
    }

    int itemLayout;

    public void setItemLayout(int layout) {
        this.itemLayout = layout;
    }

    public void setSelectedView(int position) {
        int count = mWholeLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            View item = mWholeLayout.getChildAt(i);
            View line = item.findViewById(R.id.tab_line);
            TextView tabName = (TextView) item.findViewById(R.id.tab_name);
            if (position == i) {
                line.setSelected(true);
                tabName.setSelected(true);
            } else {
                line.setSelected(false);
                tabName.setSelected(false);
            }
        }
    }

    ViewPager viewPager;

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        if (viewPager != null) {
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    setSelectedView(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }

    private TabCall tabCall;

    public TabCall getTabCall() {
        return tabCall;
    }

    public void setTabCall(TabCall tabCall) {
        this.tabCall = tabCall;
    }

    public interface TabCall {
        void item(int position,FrameLayout frameLayout);
    }

    public void setTabTitle(int position, String title) {
        View item = mWholeLayout.getChildAt(position);
        View line = item.findViewById(R.id.tab_line);
        TextView tabName = (TextView) item.findViewById(R.id.tab_name);
        tabName.setText(title);
    }

}
